%=============================================================================================
%Calibration of the model with Imperfect Competitive Deposit Market and
%Competitive lending market
% Copy right: Yu Zhu
%=============================================================================================

clear
close all
%% Read Lucas-Nicolini data
data = importdata('lucas_nicolini.csv');
rate_data = importdata('deposit_rates.csv');
name = 'LN87_08_final'
load(['pars_post_' name '.mat'])
caopt = importdata('ssopt.txt');
omega_grid = importdata('omega.txt');

data_final = data.data;
rates = rate_data.data;

% Define years used in calibration
first_year = caopt.data(1);
last_year = caopt.data(2);
%options for steady sate
ssopt = caopt.data(3); 
rq_ratio = caopt.data(5);
%=====================================
% Scatter plot of Lucas-Nicolini data
%=====================================
% LNfigure=figure
% hold on
% ID1 = data.data(:,1)>=1983;
% ID2 = data.data(:,1)<=1935;
% ID3 = logical(1-ID1-ID2);
 ID4 =  data.data(:,1)>=first_year&data.data(:,1)<=last_year;
% scatter(data.data(ID2,5),data.data(ID2,4),'red')
% scatter(data.data(ID3,5),data.data(ID3,4),'blue')
% scatter(data.data(ID1,5),data.data(ID1,4),'green')
% 
% legend( 'Before 1934','1935-1983','After 1982')
% xlabel('3m T-Bill Rate')
% ylabel('M1 to GDP Ratio')
% figure_halfpage
% saveas(LNfigure,'figures/money_demand_data','epsc')
% saveas(LNfigure,'figures/money_demand_data','png')

%============================================================================
% Restrict data period to before crisis and not immediately after 911
%============================================================================
% prepare data
data = [data_final(:,5)/100,data_final(:,4),data_final(:,1)];
data = data(ID4,:); % using regulated period with deposit rate data
ID5=rates(:,1)>=first_year&rates(:,1)<=last_year;
data = [data, rates(ID5,2),rates(ID5,3),rates(ID5,4)/100];

data = sortrows(data,1);


%============================================================================
% define variables
%============================================================================
pi_m=0.0312; % annual inflation

i_l = 0.0588555;

i_D=0.0073; % annual rate for demand deposits

online=0.2537; 
%from online shopping fraction in the survery of
%consumer payment choice Atlanta fed

% alpha1 = (1- online)*0.15;
% alpha2 = online+(1- online)*0.02;
% alpha3 = 1-alpha2-alpha1;


alpha1 = omega_grid.data(1);
alpha2 = omega_grid.data(2);
alpha3 = omega_grid.data(3);

epsilon = 0.001; % ensures the DM utility function is 0 at 0


eta = 0.66;  %Poduction function of firms f(k)=A*k^(eta)

beta = 0.96; % annual discount factor


cost=0.0297; %deposit handling cost

%================================================
%pass variables
%================================================
otherpar.beta=beta;
otherpar.epsilon = epsilon;
otherpar.cost=cost;
otherpar.pi_m=pi_m;
otherpar.spread = i_l-i_D;
otherpar.i_D = i_D;
otherpar.i_l = i_l;
otherpar.alpha1=alpha1;
otherpar.alpha2=alpha2;
otherpar.alpha3= alpha3;
otherpar.rq_ratio=rq_ratio;
otherpar.eta=eta;
%=======================================================================
%calibration
%=======================================================================


tradep = pars(4)/alpha1;
ppp = pars(11);
x = pars(1:2);
pppl = [1.4,0.9];
ppph = [1.6,0.99];
%calibrate_money_demand_fuc(.9,tradep,data)
ssopt=1;
[yyy, output,pars1, model]=calibrate_money_demand_fuc1(ppp,x,tradep,otherpar,data,ssopt);

fitted=figure
hold on
scatter(data(:,1),data(:,2),'blue')
plot(data(:,1),model,'black','linewidth',2)
ylim([0.1,0.35])
xlim([0,0.1])
legend('Data','Model')
xlabel('3m T-Bill Rate')
ylabel('M1 to GDP Ratio')
figure_halfpage
saveas(fitted,['figures/money_demand_model_' name],'epsc')                  % generate figure 5a in the paper
saveas(fitted,['figures/money_demand_model_' name ],'png')
